package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.NettyHttpClientProxyServerTransfer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.socket.NettyHttpClientProxyServerRealSocket;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * 服务端处理客户端代理到服务端的请求
 * HTTP_REPORT_CLIENT_PROXY_SERVER_TRANSFER_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyServerProxyTransferTypeAdvanced
        extends AbstractHandleHttpReportClientProxyServerTransferTypeAdvanced<NettyProxyMsg> {


    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg        通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();
        // 创建链接、发送数据
        String targetPortString = nettyProxyMsg.getTargetPortString();
        String targetIpString = nettyProxyMsg.getTargetIpString();
        byte[] data = nettyProxyMsg.getData();
        byte[] visitorId = nettyProxyMsg.getVisitorId();
        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
        // 判断代理到客户端还是服务端
        NettyHttpClientProxyServerTransfer nettyHttpClientProxyServerTransfer = new NettyHttpClientProxyServerTransfer();
        nettyHttpClientProxyServerTransfer.setSsl(false);
        nettyHttpClientProxyServerTransfer.setTargetIp(targetIpString);
        nettyHttpClientProxyServerTransfer.setTargetPort(Integer.parseInt(targetPortString));


        nettyHttpClientProxyServerTransfer.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
        nettyHttpClientProxyServerTransfer.setNettyClientProperties(nettyClientProperties);
        nettyHttpClientProxyServerTransfer.setData(data);

        NettyHttpClientProxyServerRealSocket
                .buildRealServer(nettyHttpClientProxyServerTransfer, channel,new String(visitorId));

    }

}
